<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="common/include :: header('预约养修列表')"/>
</head>
<body class="gray-bg">
<div class="container-div">
    <div class="row">
        <div class="col-sm-12 search-collapse">
            <form id="appointment-form">
                <div class="select-list">
                    <ul>
                        <li>
                            客户姓名：<input type="text" name="name"/>
                        </li>
                        <li>
                            联系方式：<input type="text" name="phone"/>
                        </li>
                        <li class="select-time">
                            <label>预约时间：</label>
                            <input type="text" class="time-input" id="startTime" placeholder="开始时间" name="beginTime"/>
                            <span>-</span>
                            <input type="text" class="time-input" id="endTime" placeholder="结束时间" name="endTime"/>
                        </li>

                        <li>
                            状态：
                            <select name="status" th:with="type=${@dict.getType('cmi_status')}">
                                <option value="">所有</option>
                                <option th:each="dict : ${type}" th:text="${dict.label}"
                                        th:value="${dict.value}"></option>
                            </select>
                        </li>
                        <li>
                            <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i
                                    class="fa fa-search"></i>&nbsp;搜索</a>
                            <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i
                                    class="fa fa-refresh"></i>&nbsp;重置</a>
                        </li>
                    </ul>
                </div>
            </form>
        </div>

        <div class="btn-group-sm" id="toolbar" role="group">
            <a class="btn btn-success" onclick="$.operate.add()" shiro:hasPermission="system:appointment:add">
                <i class="fa fa-plus"></i> 添加
            </a>
            <a class="btn btn-danger multiple disabled" onclick="$.operate.removeAll()"
               shiro:hasPermission="system:appointment:remove">
                <i class="fa fa-remove"></i> 删除
            </a>
        </div>

        <div class="col-sm-12 select-table table-striped">
            <table id="bootstrap-table"></table>
        </div>
    </div>
</div>
<th:block th:include="common/include :: footer"/>
<th:block th:include="common/include :: datetimepicker" />
<script th:inline="javascript">
    /*var editFlag = [[${@permission.hasPermi('system:appointment:edit')}]];
    var removeFlag = [[${@permission.hasPermi('system:appointment:remove')}]];*/
    var serviceTypeDatas = [[${@dict.getType('cmi_service_type')}]];
    var statusDatas = [[${@dict.getType('cmi_status')}]];
    var prefix = ctx + "/business/appointment";
    $(function () {
        var options = {
            url: prefix + "/query",
            createUrl: prefix + "/addPage",
            updateUrl: prefix + "/editPage?id={id}",
            removeUrl: prefix + "/remove",
            exportUrl: prefix + "/export",
            modalName: "预约",
            columns: [{
                checkbox: true
            },
                {
                    field: 'id',
                    title: '预约id',
                    visible: false
                },
                {
                    field: 'customerName',
                    title: '客户名称',
                    sortable: true
                },

                {
                    field: 'customerPhone',
                    title: '联系方式',
                    sortable: true
                },
                {
                    field: 'appointmentTime',
                    title: '预约时间',
                    sortable: true
                },
                {
                    field: 'actualArrivalTime',
                    title: '到店时间',
                    sortable: true
                },
                {
                    field: 'licensePlate',
                    title: '车牌号',
                    sortable: true
                },
                {
                    field: 'carSeries',
                    title: '车型',
                    sortable: true
                },
                {
                    field: 'serviceType',
                    title: '服务类型',
                    align: 'center',
                    formatter: function (value, row, index) {
                        return $.table.selectDictLabel(serviceTypeDatas, value)
                    }
                },
                {
                    field: 'info',
                    title: '备注',
                    sortable: true
                },
                {
                    field: 'status',
                    title: '状态',
                    align: 'center',
                    formatter: function (value, row, index) {
                        return $.table.selectDictLabel(statusDatas, value);
                    }
                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function (value, row, index) {
                        var actions = [];

                        //  编辑 到店 删除 取消 都只有在预约状态下才能点击
                        var editStatus = row.status == 0 || row.status == 1 ? "" : "disabled";
                        var arrivalStatus = row.status == 0 ? "" : "disabled";
                        var cancelStatus = row.status == 0 ? "" : "disabled";
                        var deleteStatus = row.status == 0 ? "" : "disabled";
                        // 结算按钮只有在到店或已支付才可以点击
                        var settleStatus = row.status == 1 || row.status == 4 ? "" : "disabled";

                        actions.push('<a class="btn btn-info btn-xs '+editStatus+'" href="javascript:void(0)" onclick="$.operate.edit(\'' + row.id + '\')">' +
                            '<i class="fa fa-edit"></i>编辑</a> ');

                        actions.push('<a class="btn btn-primary btn-xs '+arrivalStatus+'" href="javascript:void(0)" onclick="arrival(\'' + row.id + '\')">' +
                            '<i class="fa fa-sign-in"></i>到店</a> ');

                        actions.push('<a class="btn btn-warning btn-xs '+settleStatus+'" href="javascript:void(0)" onclick="statementItem(\'' + row.id + '\')">' +
                            '<i class="fa fa-remove"></i>结算单</a> ');

                        var more = [];
                        more.push("<a class='btn btn-default btn-xs "+cancelStatus+"' href='javascript:void(0)' onclick='cancelAppointment(" + row.id + ")'>" +
                            "<i class='fa fa-reply'></i>取消</a> ");

                        more.push("<a class='btn btn-danger btn-xs "+deleteStatus+"' href='javascript:void(0)' onclick='$.operate.remove(" + row.id + ")'>" +
                            "<i class='fa fa-remove'></i>删除</a>");
                        actions.push('<a tabindex="0" class="btn btn-info btn-xs" role="button" data-container="body" data-placement="left" data-toggle="popover" data-html="true" data-trigger="hover" data-content="' + more.join('') + '"><i class="fa fa-chevron-circle-right"></i>更多操作</a>');

                        return actions.join('');
                    }
                }]
        };
        $.table.init(options);
    });

    $(".myTime").datetimepicker({
        format: "yyyy-mm-dd",
        autoclose: true
    });


    function cancelAppointment(id) {
        $.modal.confirm("是否确认取消?", function () {
            $.operate.get("/appointment/cancel?id=" + id);
        });
    }

    function arrival(id) {
        $.modal.confirm("是否确认到店?", function () {
            $.operate.get("/appointment/arrival?id=" + id);
        });
    }

    function statementItem(id) {
        $.ajax({
            url: "/business/statement/isHave?appointmentId=" + id,
            type: "post",
            success: function (data) {
                console.log(data);
                if (data.code == 0) {
                    $.modal.openTab("结算单","/business/statementItem/listPage?statementId="+data.data);
                }else {
                    $.modal.confirm("是否生成结算单?", function () {
                        $.get("/business/statementItem/creatItem?appointmentId=" + id,function (data) {
                            console.log(data);
                            $.modal.openTab("结算单","/business/statementItem/listPage?statementId="+data.data);
                        });
                    });
                }
            }
        });
        /*$.modal.confirm("是否生成结算单?", function () {
            $.get("/business/statementItem/creatItem?appointmentId=" + id,function (data) {
                console.log(data);
                $.modal.openTab("结算单","/business/statementItem/listPage?statementId="+data.data);
            });
        });*/
    }

</script>
</body>
</html>